﻿using System;
using System.Management;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BTSWMILibrary
{
    public class BizTalkWebConsoleWMIQueries
    {
        //Overriden in ASP.NET project and assinged from Web.configo
        public static string BizTalkMgmtDbName = "BizTalkMgmtDb";

        public static void SetConnections(string BizTalkMgmtDB)
        {
            BizTalkMgmtDbName = BizTalkMgmtDB;
        }

        public static MSBTS_GroupSetting GetServerAndManagementDbName()
        {
            MSBTS_GroupSetting groupSettings = new MSBTS_GroupSetting();

            try
            {
                ManagementObjectSearcher searcher =
                       new ManagementObjectSearcher("root\\MicrosoftBizTalkServer",
                       "SELECT MgmtDbName,MgmtDbServerName FROM MSBTS_GroupSetting WHERE MgmtDbName = '" + BizTalkMgmtDbName + "'");

                foreach (ManagementObject queryObj in searcher.Get())
                {


                    if (queryObj["MgmtDbName"] != null)
                    {
                        groupSettings.MgmtDbName = queryObj["MgmtDbName"].ToString();
                    }

                    if (queryObj["MgmtDbServerName"] != null)
                    {
                        groupSettings.MgmtDbServerName = queryObj["MgmtDbServerName"].ToString();
                    }

                    #region ExtraFields
                    /*
                    if (queryObj["BamDBName"] != null)
                    {
                        groupSettings.BamDBName = queryObj["BamDBName"].ToString();
                    }
                    if (queryObj["BamDBServerName"] != null)
                    {
                        groupSettings.BamDBServerName = queryObj["BamDBServerName"].ToString();
                    }
                    if (queryObj["BizTalkAdministratorGroup"] != null)
                    {
                        groupSettings.BizTalkAdministratorGroup = queryObj["BizTalkAdministratorGroup"].ToString();
                    }
                    if (queryObj["BizTalkB2BOperatorGroup"] != null)
                    {
                        groupSettings.BizTalkB2BOperatorGroup = queryObj["BizTalkB2BOperatorGroup"].ToString();
                    }
                    if (queryObj["BizTalkOperatorGroup"] != null)
                    {
                        groupSettings.BizTalkOperatorGroup = queryObj["BizTalkOperatorGroup"].ToString();
                    }

                    if (queryObj["Caption"] != null)
                    {
                        groupSettings.Caption = queryObj["Caption"].ToString();
                    }

                    if (queryObj["ConfigurationCacheRefreshInterval"] != null)
                    {
                        groupSettings.ConfigurationCacheRefreshInterval = queryObj["ConfigurationCacheRefreshInterval"].ToString();
                    }

                    if (queryObj["Description"] != null)
                    {
                        groupSettings.Description = queryObj["Description"].ToString();
                    }

                    if (queryObj["GlobalTrackingOption"] != null)
                    {
                        groupSettings.GlobalTrackingOption = queryObj["GlobalTrackingOption"].ToString();
                    }

                    if (queryObj["LMSFragmentSize"] != null)
                    {
                        groupSettings.LMSFragmentSize = queryObj["LMSFragmentSize"].ToString();
                    }
                    if (queryObj["LMSThreshold"] != null)
                    {
                        groupSettings.LMSThreshold = queryObj["LMSThreshold"].ToString();
                    }
                    if (queryObj["MajorVersion"] != null)
                    {
                        groupSettings.MajorVersion = queryObj["MajorVersion"].ToString();
                    }*/



                    /*
                    if (queryObj["MinorVersion"] != null)
                    {
                        groupSettings.MinorVersion = queryObj["MinorVersion"].ToString();
                    }
                    if (queryObj["Name"] != null)
                    {
                        groupSettings.Name = queryObj["Name"].ToString();
                    }

                    if (queryObj["PerfCounterCacheRefreshInterval"] != null)
                    {
                        groupSettings.PerfCounterCacheRefreshInterval = queryObj["PerfCounterCacheRefreshInterval"].ToString();
                    }
                    if (queryObj["RuleEngineDBName"] != null)
                    {
                        groupSettings.RuleEngineDBName = queryObj["RuleEngineDBName"].ToString();
                    }
                    if (queryObj["RuleEngineDBServerName"] != null)
                    {
                        groupSettings.RuleEngineDBServerName = queryObj["RuleEngineDBServerName"].ToString();
                    }
                    if (queryObj["SettingID"] != null)
                    {
                        groupSettings.SettingID = queryObj["SettingID"].ToString();
                    }
                    if (queryObj["SignCertComment"] != null)
                    {
                        groupSettings.SignCertComment = queryObj["SignCertComment"].ToString();
                    }

                    if (queryObj["SignCertThumbprint"] != null)
                    {
                        groupSettings.SignCertThumbprint = queryObj["SignCertThumbprint"].ToString();
                    }
                    if (queryObj["SSOServerName"] != null)
                    {
                        groupSettings.SSOServerName = queryObj["SSOServerName"].ToString();
                    }
                    if (queryObj["SubscriptionDBName"] != null)
                    {
                        groupSettings.SubscriptionDBName = queryObj["SubscriptionDBName"].ToString();
                    }
                    if (queryObj["SubscriptionDBServerName"] != null)
                    {
                        groupSettings.SubscriptionDBServerName = queryObj["SubscriptionDBServerName"].ToString();
                    }
                    if (queryObj["TrackingAnalysisDBName"] != null)
                    {
                        groupSettings.TrackingAnalysisDBName = queryObj["TrackingAnalysisDBName"].ToString();
                    }
                    if (queryObj["TrackingAnalysisDBServerName"] != null)
                    {
                        groupSettings.TrackingAnalysisDBServerName = queryObj["TrackingAnalysisDBServerName"].ToString();
                    }
                    if (queryObj["TrackingDBName"] != null)
                    {
                        groupSettings.TrackingDBName = queryObj["TrackingDBName"].ToString();
                    }
                    if (queryObj["TrackingDBServerName"] != null)
                    {
                        groupSettings.TrackingDBServerName = queryObj["TrackingDBServerName"].ToString();
                    }*/

                    #endregion
                }
            }
            catch (ManagementException e)
            {
                //MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }

            return groupSettings;
        }

        public static List<MSBTS_Host> GetHosts()
        {
            List<MSBTS_Host> hostList = new List<MSBTS_Host>();
            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\MicrosoftBizTalkServer",
                    "SELECT * FROM MSBTS_Host");
                MSBTS_Host host;

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    host = new MSBTS_Host();
                    host.AuthTrusted = queryObj["AuthTrusted"].ToString();
                    //host.Caption = queryObj["Caption"].ToString();
                    host.ClusterResourceGroupName = queryObj["ClusterResourceGroupName"].ToString();
                    host.DecryptCertComment = queryObj["DecryptCertComment"].ToString();
                    host.DecryptCertThumbprint = queryObj["DecryptCertThumbprint"].ToString();
                    //host.Description = queryObj["Description"].ToString();
                    host.HostTracking = queryObj["HostTracking"].ToString();
                    host.HostType = queryObj["HostType"].ToString();
                    //host.InstallDate = queryObj["InstallDate"].ToString();
                    host.IsDefault = queryObj["IsDefault"].ToString();
                    //host.LastUsedLogon = queryObj["LastUsedLogon"].ToString();
                    //host.MgmtDbNameOverride = queryObj["MgmtDbNameOverride"].ToString();
                    //host.MgmtDbServerOverride = queryObj["MgmtDbServerOverride"].ToString();
                    host.Name = queryObj["Name"].ToString();
                    host.NTGroupName = queryObj["NTGroupName"].ToString();
                    //host.Status = queryObj["Status"].ToString();

                    hostList.Add(host);
                }


            }
            catch (ManagementException e)
            {

            }

            return hostList;
        }

        public static List<MSBTS_HostInstance> GetHostInstances()
        {
            List<MSBTS_HostInstance> hostInstances = new List<MSBTS_HostInstance>();
            MSBTS_HostInstance hostInstance = null;

            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\MicrosoftBizTalkServer",
                    "SELECT * FROM MSBTS_HostInstance");

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    hostInstance = new MSBTS_HostInstance();

                    if (queryObj["Caption"] != null)
                    {
                        hostInstance.Caption = queryObj["Caption"].ToString();
                    }
                    if (queryObj["ClusterInstanceType"] != null)
                    {
                        hostInstance.ClusterInstanceType = queryObj["ClusterInstanceType"].ToString();
                    }
                    if (queryObj["ConfigurationState"] != null)
                    {
                        hostInstance.ConfigurationState = queryObj["ConfigurationState"].ToString();
                    }
                    if (queryObj["Description"] != null)
                    {
                        hostInstance.Description = queryObj["Description"].ToString();
                    }
                    if (queryObj["HostName"] != null)
                    {
                        hostInstance.HostName = queryObj["HostName"].ToString();
                    }
                    if (queryObj["HostType"] != null)
                    {
                        hostInstance.HostType = queryObj["HostType"].ToString();
                    }
                    if (queryObj["InstallDate"] != null)
                    {
                        hostInstance.InstallDate = queryObj["InstallDate"].ToString();
                    }
                    if (queryObj["IsDisabled"] != null)
                    {
                        hostInstance.IsDisabled = queryObj["IsDisabled"].ToString();
                    }
                    if (queryObj["Logon"] != null)
                    {
                        hostInstance.Logon = queryObj["Logon"].ToString();
                    }
                    if (queryObj["MgmtDbNameOverride"] != null)
                    {
                        hostInstance.MgmtDbNameOverride = queryObj["MgmtDbNameOverride"].ToString();
                    }
                    if (queryObj["MgmtDbServerOverride"] != null)
                    {
                        hostInstance.MgmtDbServerOverride = queryObj["MgmtDbServerOverride"].ToString();
                    }
                    if (queryObj["Name"] != null)
                    {
                        hostInstance.Name = queryObj["Name"].ToString();
                    }
                    if (queryObj["NTGroupName"] != null)
                    {
                        hostInstance.NTGroupName = queryObj["NTGroupName"].ToString();
                    }
                    if (queryObj["RunningServer"] != null)
                    {
                        hostInstance.RunningServer = queryObj["RunningServer"].ToString();
                    }
                    if (queryObj["ServiceState"] != null)
                    {
                        hostInstance.ServiceState = queryObj["ServiceState"].ToString();
                    }

                    if (queryObj["Status"] != null)
                    {
                        hostInstance.Status = queryObj["Status"].ToString();
                    }

                    if (queryObj["UniqueID"] != null)
                    {
                        hostInstance.UniqueID = queryObj["UniqueID"].ToString();
                    }


                    hostInstances.Add(hostInstance);
                }
            }
            catch (ManagementException e)
            {

            }

            return hostInstances;
        }

        public static MSBTS_MsgBoxSetting GetMsgBoxSettings()
        {
            MSBTS_MsgBoxSetting msgBoxSetting = new MSBTS_MsgBoxSetting();


            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\MicrosoftBizTalkServer",
                    "SELECT * FROM MSBTS_MsgBoxSetting");

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    if (queryObj["Description"] != null)
                    {
                        msgBoxSetting.Description = queryObj["Description"].ToString();
                    }

                    if (queryObj["DisableNewMessagePublication"] != null)
                    {
                        msgBoxSetting.DisableNewMessagePublication = queryObj["DisableNewMessagePublication"].ToString();
                    }

                    if (queryObj["IsMasterMsgBox"] != null)
                    {
                        msgBoxSetting.IsMasterMsgBox = queryObj["IsMasterMsgBox"].ToString();
                    }

                    if (queryObj["MgmtDbNameOverride"] != null)
                    {
                        msgBoxSetting.MgmtDbNameOverride = queryObj["MgmtDbNameOverride"].ToString();
                    }

                    if (queryObj["MgmtDbServerOverride"] != null)
                    {
                        msgBoxSetting.MgmtDbServerOverride = queryObj["MgmtDbServerOverride"].ToString();
                    }

                    if (queryObj["MsgBoxDBName"] != null)
                    {
                        msgBoxSetting.MsgBoxDBName = queryObj["MsgBoxDBName"].ToString();
                    }

                    if (queryObj["MsgBoxDBServerName"] != null)
                    {
                        msgBoxSetting.MsgBoxDBServerName = queryObj["MsgBoxDBServerName"].ToString();
                    }

                    if (queryObj["SettingID"] != null)
                    {
                        msgBoxSetting.SettingID = queryObj["SettingID"].ToString();
                    }
                }
            }
            catch (ManagementException e)
            {

            }

            return msgBoxSetting;
        }

        public static List<MSBTS_AdapterSetting> GetAdapterSettings()
        {
            ManagementObjectSearcher searcher =
                   new ManagementObjectSearcher("root\\MicrosoftBizTalkServer",
                    "SELECT * FROM MSBTS_AdapterSetting");

            MSBTS_AdapterSetting adapterSettingObj;
            List<MSBTS_AdapterSetting> adapterSettingList = new List<MSBTS_AdapterSetting>();

            foreach (ManagementObject queryObj in searcher.Get())
            {
                adapterSettingObj = new MSBTS_AdapterSetting();
                adapterSettingObj.Name = queryObj["Name"].ToString();
                adapterSettingList.Add(adapterSettingObj);
            }

            return adapterSettingList;
        }
    }
}
